package com.lhj.crm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhj.crm.entity.Department;
import com.lhj.crm.entity.Role;
import com.lhj.crm.service.IDepartmentService;
import com.lhj.crm.service.impl.DepartmentServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author FYI
 * @since 2020-03-24
 */
@RestController
public class DepartmentController {

    private Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/department")
    public List<Department> department(){

        return departmentService.list();
    }
    @GetMapping("/department/ListPage")
    public Map getList(Integer page, Integer limit ){
        Map<String,Object> map = new HashMap<String,Object>();

        //设置mybatisPlus分页
        Page<Department> deptPage = new Page<Department>();
        //设置每页记录数
        deptPage.setSize(limit);
        //设置当前页码
        deptPage.setCurrent(page);

        IPage<Department> iPage = departmentService.selectPageList(deptPage);

        map.put("code",0);
        map.put("msg","succeed");
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());

        return map;
    }

    @PostMapping("/department")
    public Map DeptAdd(Department department){
        Map<String,Object> map = new HashMap<String,Object>();
        Integer add = departmentService.add(department);
        map.put("state",add);
        return map;
    }
    @PutMapping("/department")
    public Map RoleUpdate(Department department) {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer update = departmentService.update(department);
        map.put("state",update);
        return map;
    }


    @DeleteMapping("/department/del/{deptId}")
    public Map RoleDel(@PathVariable Integer deptId){
        Department department = new Department();
        department.setDeptId(deptId);
        department.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",departmentService.updateById(department));
        return result;
    }
}
